package fr.epsi.activities.absence;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.example.MyLittleHotel.R;
import fr.epsi.BDD.BDD;
import fr.epsi.fragment.pickers.DatePickerFragment;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Florent Baudon on 28/05/2014.
 */
public class AjouterAbsenceActivity extends FragmentActivity implements View.OnClickListener,DatePickerFragment.ValidDateSetListener {

    Spinner emp;
    EditText motif, debut, fin;
    Button ajouter;
    String empSelect;
    DialogFragment dateDeb, dateFin;
    BDD bdd;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajouterabsencelayout);

        emp = (Spinner)findViewById(R.id.empAbs);
        List<String> empList = setEmpService();
        ArrayAdapter<String> spinnerAdaptEmp = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, empList);
        spinnerAdaptEmp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        emp.setAdapter(spinnerAdaptEmp);
        emp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                empSelect = (String)adapterView.getItemAtPosition(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        motif = (EditText)findViewById(R.id.motifAbs);
        debut = (EditText)findViewById(R.id.dateAbsDeb);
        debut.setOnClickListener(this);
        fin = (EditText)findViewById(R.id.dateAbsFin);
        fin.setOnClickListener(this);

        ajouter = (Button)findViewById(R.id.ajouterAbs);
        ajouter.setOnClickListener(this);
    }

    public void validDateSet(DialogFragment df, int an, int mois, int jour) {
        //car android commence le calendrier au mois 0, donc janvier = 0, fevriers=1 etc...
        mois++;
        String m = String.valueOf(mois);
        String j = String.valueOf(jour);

        if(m.length()==1)
            m = "0" + m;
        if(j.length()==1)
            j = "0" + j;
        if(df==dateDeb)
            debut.setText(an + "/" + m + "/" + j);
        if(df==dateFin)
            fin.setText(an + "/" + m + "/" + j);
    }

    @Override
    public void onClick(View v) {
        if(v==debut){
            dateDeb = new DatePickerFragment();
            FragmentManager fm = getSupportFragmentManager();
            dateDeb.show(fm, "Date Picker");
        }
        if(v==fin){
            dateFin = new DatePickerFragment();
            FragmentManager fm = getSupportFragmentManager();
            dateFin.show(fm, "Time Picker");
        }
        if(v==ajouter){
            bdd = new BDD();
            //Separation nom et prenom
            String nom = empSelect.substring(0, empSelect.indexOf(" "));
            String prenom = empSelect.substring(empSelect.indexOf(" ")+1, empSelect.length());

            //insertion dans la base de donnée
            try {
                bdd.open(this);
                bdd.insertAbsences(nom, prenom, motif.getText().toString(), debut.getText().toString() + " 00:00", fin.getText().toString() + " 23:59");
            }catch (SQLException e){
                e.printStackTrace();
            }
            motif.setText("");
            debut.setText("");
            fin.setText("");

            Toast t = Toast.makeText(this, "Absence ajoutée", Toast.LENGTH_SHORT);
            t.show();
        }
    }

    //récupére la liste des service pour le spinner
    private List<String> setEmpService(){

        bdd = new BDD();
        Cursor cListEmp;
        try{
            bdd.open(this);
        }catch (SQLException e){
            e.printStackTrace();
        }
        cListEmp = bdd.getEmployes();

        List<String> serviceList = new ArrayList<String>();

        if(cListEmp!=null && cListEmp.moveToFirst()){
            boolean finish=false;
            while(!finish) {
                serviceList.add(cListEmp.getString(1) + " " + cListEmp.getString(2));
                if (cListEmp.isLast())
                    finish = true;
                else
                    cListEmp.moveToNext();
            }
        }
        return serviceList;
    }
}